package config

import (
	"io/ioutil"

	"gopkg.in/yaml.v2"
)

var configFileName string

type Config struct {
	Listen      string             `yaml:"listen"`
	Port        int                `yaml:"port"`
	Salt        string             `yaml:"salt"`
	Key         string             `yaml:"key"`
	LogLevel    string             `yaml:"log_level"`
	Subscribers []SubscriberConfig `yaml:"subscribers"`
	Peoples     []PeopleConfig     `yaml:"peoples"`
}

type PeopleConfig struct {
	Name  string `yaml:"name"`
	Email string `yaml:"email"`
}

type SubscriberConfig struct {
	Name string `yaml:"name"`
	Url  string `yaml:"url"`
}

func ParseConfigData(data []byte) (*Config, error) {
	var cfg Config
	if err := yaml.Unmarshal(data, &cfg); err != nil {
		return nil, err
	}
	return &cfg, nil
}

func ParseConfigFile(fileName string) (*Config, error) {
	data, err := ioutil.ReadFile(fileName)
	if err != nil {
		return nil, err
	}

	configFileName = fileName

	return ParseConfigData(data)
}
